home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDBC_011 / JAVA / SQL / CONNECTI.JAV < prev    next >
Encoding:
Text File  |  1996-11-10  |  10.6 KB  |  301 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "LICENSE"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  19.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  20.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  21.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  22.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  23.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  24.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  25.  * HIGH RISK ACTIVITIES.
  26.  */
  27.  
  28. package java.sql;
  29.  
  30. /**
  31.  * <P>A Connection represents a session with a specific
  32.  * database. Within the context of a Connection, SQL statements are
  33.  * executed and results are returned.
  34.  *
  35.  * <P>A Connection's database is able to provide information
  36.  * describing its tables, its supported SQL grammar, its stored
  37.  * procedures, the capabilities of this connection, etc. This
  38.  * information is obtained with the getMetaData method.
  39.  *
  40.  * <P><B>Note:</B> By default the Connection automatically commits
  41.  * changes after executing each statement. If auto commit has been
  42.  * disabled, an explicit commit must be done or database changes will
  43.  * not be saved.
  44.  *
  45.  * @see DriverManager#getConnection
  46.  * @see Statement 
  47.  * @see ResultSet
  48.  * @see DatabaseMetaData
  49.  */
  50. public interface Connection {
  51.  
  52.     /**
  53.      * SQL statements without parameters are normally
  54.      * executed using Statement objects. If the same SQL statement 
  55.      * is executed many times, it is more efficient to use a 
  56.      * PreparedStatement
  57.      *
  58.      * @return a new Statement object 
  59.      */
  60.     Statement createStatement() throws SQLException;
  61.  
  62.     /**
  63.      * A SQL statement with or without IN parameters can be
  64.      * pre-compiled and stored in a PreparedStatement object. This
  65.      * object can then be used to efficiently execute this statement
  66.      * multiple times.
  67.      *
  68.      * <P><B>Note:</B> This method is optimized for handling
  69.      * parametric SQL statements that benefit from precompilation. If
  70.      * the driver supports precompilation, prepareStatement will send
  71.      * the statement to the database for precompilation. Some drivers
  72.      * may not support precompilation. In this case, the statement may
  73.      * not be sent to the database until the PreparedStatement is
  74.      * executed.  This has no direct affect on users; however, it does
  75.      * affect which method throws certain SQLExceptions.
  76.      *
  77.      * @param sql a SQL statement that may contain one or more '?' IN
  78.      * parameter placeholders
  79.      *
  80.      * @return a new PreparedStatement object containing the
  81.      * pre-compiled statement 
  82.      */
  83.     PreparedStatement prepareStatement(String sql)
  84.         throws SQLException;
  85.  
  86.     /**
  87.      * A SQL stored procedure call statement is handled by creating a
  88.      * CallableStatement for it. The CallableStatement provides
  89.      * methods for setting up its IN and OUT parameters, and
  90.      * methods for executing it.
  91.      *
  92.      * <P><B>Note:</B> This method is optimized for handling stored
  93.      * procedure call statements. Some drivers may send the call
  94.      * statement to the database when the prepareCall is done; others
  95.      * may wait until the CallableStatement is executed. This has no
  96.      * direct affect on users; however, it does affect which method
  97.      * throws certain SQLExceptions.
  98.      *
  99.      * @param sql a SQL statement that may contain one or more '?'
  100.      * parameter placeholders. Typically this  statement is a JDBC
  101.      * function call escape string.
  102.      *
  103.      * @return a new CallableStatement object containing the
  104.      * pre-compiled SQL statement 
  105.      */
  106.     CallableStatement prepareCall(String sql) throws SQLException;
  107.                         
  108.     /**
  109.      * A driver may convert the JDBC sql grammar into its system's
  110.      * native SQL grammar prior to sending it; nativeSQL returns the
  111.      * native form of the statement that the driver would have sent.
  112.      *
  113.      * @param sql a SQL statement that may contain one or more '?'
  114.      * parameter placeholders
  115.      *
  116.      * @return the native form of this statement
  117.      */
  118.     String nativeSQL(String sql) throws SQLException;
  119.  
  120.     /**
  121.      * If a connection is in auto-commit mode, then all its SQL
  122.      * statements will be executed and committed as individual
  123.      * transactions.  Otherwise, its SQL statements are grouped into
  124.      * transactions that are terminated by either commit() or
  125.      * rollback().  By default, new connections are in auto-commit
  126.      * mode.
  127.      *
  128.      * The commit occurs when the statement completes or the next
  129.      * execute occurs, whichever comes first. In the case of
  130.      * statements returning a ResultSet, the statement completes when
  131.      * the last row of the ResultSet has been retrieved or the
  132.      * ResultSet has been closed. In advanced cases, a single
  133.      * statement may return multiple results as well as output
  134.      * parameter values. Here the commit occurs when all results and
  135.      * output param values have been retrieved.
  136.      *
  137.      * @param autoCommit true enables auto-commit; false disables
  138.      * auto-commit.  
  139.      */
  140.     void setAutoCommit(boolean autoCommit) throws SQLException;
  141.  
  142.     /**
  143.      * Get the current auto-commit state.
  144.      * @return Current state of auto-commit mode.
  145.      * @see #setAutoCommit 
  146.      */
  147.     boolean getAutoCommit() throws SQLException;
  148.  
  149.     /**
  150.      * Commit makes all changes made since the previous
  151.      * commit/rollback permanent and releases any database locks
  152.      * currently held by the Connection. This method should only be
  153.      * used when auto commit has been disabled.
  154.      *
  155.      * @see #setAutoCommit 
  156.      */
  157.     void commit() throws SQLException;
  158.  
  159.     /**
  160.      * Rollback drops all changes made since the previous
  161.      * commit/rollback and releases any database locks currently held
  162.      * by the Connection. This method should only be used when auto
  163.      * commit has been disabled.
  164.      *
  165.      * @see #setAutoCommit 
  166.      */
  167.     void rollback() throws SQLException;
  168.  
  169.     /**
  170.      * In some cases, it is desirable to immediately release a
  171.      * Connection's database and JDBC resources instead of waiting for
  172.      * them to be automatically released; the close method provides this
  173.      * immediate release. 
  174.      *
  175.      * <P><B>Note:</B> A Connection is automatically closed when it is
  176.      * garbage collected. Certain fatal errors also result in a closed
  177.      * Connection.
  178.      */
  179.     void close() throws SQLException;;
  180.  
  181.     /**
  182.      * Tests to see if a Connection is closed.
  183.      *
  184.      * @return true if the connection is closed; false if it's still open
  185.      */
  186.     boolean isClosed() throws SQLException;;
  187.  
  188.     //======================================================================
  189.     // Advanced features:
  190.  
  191.     /**
  192.      * A Connection's database is able to provide information
  193.      * describing its tables, its supported SQL grammar, its stored
  194.      * procedures, the capabilities of this connection, etc. This
  195.      * information is made available through a DatabaseMetaData
  196.      * object.
  197.      *
  198.      * @return a DatabaseMetaData object for this Connection 
  199.      */
  200.     DatabaseMetaData getMetaData() throws SQLException;;
  201.  
  202.     /**
  203.      * You can put a connection in read-only mode as a hint to enable 
  204.      * database optimizations.
  205.      *
  206.      * <P><B>Note:</B> setReadOnly cannot be called while in the
  207.      * middle of a transaction.
  208.      *
  209.      * @param readOnly true enables read-only mode; false disables
  210.      * read-only mode.  
  211.      */
  212.     void setReadOnly(boolean readOnly) throws SQLException;
  213.  
  214.     /**
  215.      * Tests to see if the connection is in read-only mode.
  216.      *
  217.      * @return true if connection is read-only
  218.      */
  219.     boolean isReadOnly() throws SQLException;
  220.  
  221.     /**
  222.      * A sub-space of this Connection's database may be selected by setting a
  223.      * catalog name. If the driver does not support catalogs it will
  224.      * silently ignore this request.
  225.      */
  226.     void setCatalog(String catalog) throws SQLException;
  227.  
  228.     /**
  229.      * Return the Connection's current catalog name.
  230.      *
  231.      * @return the current catalog name or null
  232.      */
  233.     String getCatalog() throws SQLException;
  234.  
  235.     /**
  236.      * Transactions are not supported. 
  237.      */
  238.     int TRANSACTION_NONE         = 0;
  239.  
  240.     /**
  241.      * Dirty reads, non-repeatable reads and phantom reads can occur.
  242.      */
  243.     int TRANSACTION_READ_UNCOMMITTED = 1;
  244.  
  245.     /**
  246.      * Dirty reads are prevented; non-repeatable reads and phantom
  247.      * reads can occur.
  248.      */
  249.     int TRANSACTION_READ_COMMITTED   = 2;
  250.  
  251.     /**
  252.      * Dirty reads and non-repeatable reads are prevented; phantom
  253.      * reads can occur.     
  254.      */
  255.     int TRANSACTION_REPEATABLE_READ  = 4;
  256.  
  257.     /**
  258.      * Dirty reads, non-repeatable reads and phantom reads are prevented.
  259.      */
  260.     int TRANSACTION_SERIALIZABLE     = 8;
  261.  
  262.     /**
  263.      * You can call this method to try to change the transaction
  264.      * isolation level using one of the TRANSACTION_* values.
  265.      *
  266.      * <P><B>Note:</B> setTransactionIsolation cannot be called while
  267.      * in the middle of a transaction.
  268.      *
  269.      * @param level one of the TRANSACTION_* isolation values with the
  270.      * exception of TRANSACTION_NONE; some databases may not support
  271.      * other values
  272.      *
  273.      * @see DatabaseMetaData#supportsTransactionIsolationLevel 
  274.      */
  275.     void setTransactionIsolation(int level) throws SQLException;
  276.  
  277.     /**
  278.      * Get this Connection's current transaction isolation mode.
  279.      *
  280.      * @return the current TRANSACTION_* mode value
  281.      */
  282.     int getTransactionIsolation() throws SQLException;
  283.  
  284.     /**
  285.      * The first warning reported by calls on this Connection is
  286.      * returned.  
  287.      *
  288.      * <P><B>Note:</B> Subsequent warnings will be chained to this
  289.      * SQLWarning.
  290.      *
  291.      * @return the first SQLWarning or null 
  292.      */
  293.     SQLWarning getWarnings() throws SQLException;
  294.  
  295.     /**
  296.      * After this call, getWarnings returns null until a new warning is
  297.      * reported for this Connection.  
  298.      */
  299.     void clearWarnings() throws SQLException;
  300. }
  301.